package com.sanyou.controller;

import com.sanyou.pojo.Equipment;
import com.sanyou.pojo.Factory;
import com.sanyou.pojo.vo.EquipmentVo;
import com.sanyou.service.EquipmentService;
import com.sanyou.service.FactoryService;
import com.sanyou.utils.JSONResult;
import com.sanyou.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sanyou.controller.BasicController.PAGE_SIZE;

/**
 * User: asus
 * Date: 2021/6/5
 * Time: 17:37
 * Version:V1.0
 */
@Api(value = "设备相关的接口",tags = {"设备相关的Controller"})
@RestController
@CrossOrigin
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private FactoryService factoryService;
    /**
     * 新增设备，必填字段：设备编号，厂家名称，生产线
     * @param equipment
     * @return
     */
    @ApiOperation(value = "新增设备", notes = "新增设备")
    @PostMapping("/addEquipment")
    public JSONResult addEquipment(@RequestBody Equipment equipment){

    if (equipment == null || StringUtils.isBlank(equipment.getEquipNo()))
        return JSONResult.errorMsg("设备编号不允许为空!");

    if (StringUtils.isBlank(equipment.getFactoryId()))
        return JSONResult.errorMsg("厂家名称不允许为空!");

    if (StringUtils.isBlank(equipment.getSubFactoryId()))
        return JSONResult.errorMsg("生产线不允许为空!");

/*    if (equipment.getEquipCycle() == null)
        return JSONResult.errorMsg("阴极周期不允许为空!");

     if ( equipment.getEquipType()!=null&&equipment.getEquipType()==0&&equipment.getEquipHealthLimit() == null)
        return JSONResult.errorMsg("健康临界点不允许为空!");

    if (equipment.getEquipType()!=null&&equipment.getEquipType()==0&&equipment.getEquipSubhealthLimit() == null)
        return JSONResult.errorMsg("亚健康临界点不允许为空!"); */

    equipmentService.addEquipment(equipment);

    return JSONResult.ok();
    }

    /**
     * 分页查看设备信息，deletemark为0
     * @param equipmentVo
     * @param page
     * @param pageSize
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name="page",value = "分页数",required = true,
            dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false,
                    dataType = "int", paramType = "query")})
    @ApiOperation(value = "查看设备列表", notes = "查看设备列表")
    @PostMapping("/query")
    public JSONResult query(@RequestBody EquipmentVo equipmentVo, Integer page, Integer pageSize){

        System.out.println("设备类型："+equipmentVo.getEquipType());
        if(page == null)
            page = 1;

        if(pageSize == null)
            pageSize = PAGE_SIZE;

        PagedResult pagedResult = equipmentService.query(equipmentVo,page,pageSize);

        return JSONResult.ok(pagedResult);
    }
  /*  @ApiImplicitParams({@ApiImplicitParam(name="page",value = "分页数",required = true,
            dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false,
                    dataType = "int", paramType = "query")})
    @ApiOperation(value = "查看设备列表", notes = "查看设备列表")
    @PostMapping("/querySearch")
    public JSONResult querySearch(@RequestBody EquipmentVo equipmentVo, Integer page, Integer pageSize){

        if(page == null)
            page = 1;

        if(pageSize == null)
            pageSize = PAGE_SIZE;

        PagedResult pagedResult = equipmentService.querySearch(equipmentVo,page,pageSize);

        return JSONResult.ok(pagedResult);
    }*/
    /**
     * 根据设备id启用/禁用设备
     * @param id
     * @param mark 1：启用   0：禁用
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "设备id", required = true,
            dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "mark", value = "启用/禁用标志", required = true,
                    dataType = "int", paramType = "query")})
    @ApiOperation(value = "启用/禁用设备", notes = "启用/禁用设备")
    @PostMapping("/enableOrUnEnableEquip")
    public JSONResult enableOrUnEnableEquip(String id , int mark){
        if(StringUtils.isBlank(id))
            return JSONResult.errorMsg("设备id为空");

        equipmentService.enableOrUnEnableEquip(id, mark);

        return JSONResult.ok();
    }

    /**
     * 更新设备信息并记录变更记录，必填字段：生产线，设备编号，厂家名称，用户id
     * @param equipment
     * @param userId
     * @return
     */
    @ApiImplicitParam(name = "userId", value = "用户id", required = true,
            dataType = "string", paramType = "query")
    @ApiOperation(value = "修改设备信息",notes = "修改设备信息")
    @PostMapping("/updateEquipInfo")
    public JSONResult updateEquipInfo(@RequestBody Equipment equipment,String userId){
        System.out.println( "设备类型 "+equipment.getEquipType());

        if(StringUtils.isBlank(userId))
            return JSONResult.errorMsg("用户id不能为空!");

        if(equipment == null || StringUtils.isBlank(equipment.getEquipNo()))
            return JSONResult.errorMsg("设备编号不允许为空!");

        if(StringUtils.isBlank(equipment.getFactoryId()))
            return JSONResult.errorMsg("厂家名称不允许为空!");

        if(StringUtils.isBlank(equipment.getSubFactoryId()))
            return JSONResult.errorMsg("生产线不允许为空!");

/*         if(equipment.getEquipCycle() == null)
            return JSONResult.errorMsg("阴极周期不允许为空!");

        if( equipment.getEquipType()!=null&&equipment.getEquipType()==0&&equipment.getEquipHealthLimit() == null)
            return JSONResult.errorMsg("健康临界点不允许为空!");

        if( equipment.getEquipType()!=null&&equipment.getEquipType()==0&& equipment.getEquipSubhealthLimit() == null)
            return JSONResult.errorMsg("亚健康临界点不允许为空!"); */


        equipmentService.updateEquipInfo(equipment,userId);

        return JSONResult.ok();
    }
    /**
     * 删除设备，实际操作是delete_mark置1
     * @param equipment
     * @return
     */
    @ApiOperation(value = "删除设备",notes = "删除设备")
    @PostMapping("/deleteEquip")
    public JSONResult deleteEquip(@RequestBody Equipment equipment){

        if(equipment == null || StringUtils.isBlank(equipment.getId()))
            return JSONResult.errorMsg("设备ID不允许为空!");

        equipmentService.deleteEquip(equipment);

        return JSONResult.ok();
    }
    /**
     * 更新设备地址
     * @param equipment
     * @return
     */
    @ApiOperation(value = "更新设备地址",notes = "更新设备地址")
    @PostMapping("/updateAddress")
    public JSONResult updateAddress(@RequestBody Equipment equipment){
        if(equipment == null || StringUtils.isBlank(equipment.getId()))
            return JSONResult.errorMsg("设备ID不允许为空!");

        equipmentService.updateAddress(equipment);

        return JSONResult.ok();
    }
    /**
     * 未使用
     */
    @ApiOperation(value = "查询所有设备,生成树结构返回", notes = "查询所有设备,生成树结构返回")
    @GetMapping("/getEquipmentTree")
    public JSONResult getEquipmentTree(){

        List<EquipmentVo> list = equipmentService.getEquipmentTree();

        return JSONResult.ok(list);
    }

    /**
     * 查询用户拥有的设备权限
     * @param equipType
     * @param userId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户id",required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name="equipType",value = "设备类型",required = true, dataType = "string", paramType = "query")
    })
    @ApiOperation(value = "查询用户拥有的设备权限", notes = "查询用户拥有的设备权限")
    @GetMapping("/getUserEquip")
    public JSONResult getUserEquip(String userId,Byte equipType ){
        System.out.println("设备类型："+equipType);
        System.out.println("用户："+userId);

        if(StringUtils.isBlank(userId))
            return JSONResult.errorMsg("用户id为空!");

        List<Equipment> equipmentList = equipmentService.getUserEquip(userId,equipType);

        return JSONResult.ok(equipmentList);
    }
    /**
     * 查询用户拥有的厂家权限
     * @param userId
     * @return
     */
    @ApiImplicitParam(name = "userId", value = "用户id", required = true,
            dataType = "string", paramType = "query")
    @ApiOperation(value = "查询用户拥有的厂家权限", notes = "查询用户拥有的厂家权限")
    @GetMapping("/getUserEquipFactory")
    public JSONResult getUserEquipFactory(String userId){
        if(StringUtils.isBlank(userId))
            return JSONResult.errorMsg("用户id为空!");

        List<Factory> factoryList = factoryService.getUserEquipFactory(userId);

        return JSONResult.ok(factoryList);
    }
    /**
     * 查询厂家拥有的设备,每台设备都有所属厂家id
     * @param factoryId
     * @return
     */
    @ApiImplicitParam(name = "factoryId", value = "厂家id", required = true,
            dataType = "string", paramType = "query")
    @ApiOperation(value = "查询厂家拥有的设备", notes = "查询厂家拥有的设备")
    @GetMapping("/getEquip")
    public JSONResult getEquip(String factoryId){

        if(StringUtils.isBlank(factoryId))
            return JSONResult.errorMsg("厂家id为空");

        List<Equipment> equipmentList = equipmentService.getEquip(factoryId);

        return JSONResult.ok(equipmentList);
    }
    /**
     * 查询车间拥有的设备,每台设备都有所属车间id
     * @param subFactoryId
     * @return
     */
    @ApiImplicitParam(name = "subFactoryId", value = "车间id", required = true,
            dataType = "string", paramType = "query")
    @ApiOperation(value = "查询车间拥有的设备", notes = "查询车间拥有的设备")
    @GetMapping("/getEquipBySubFactoryId")
    public JSONResult getEquipBySubFactoryId(String subFactoryId){

        if(StringUtils.isBlank(subFactoryId))
            return JSONResult.errorMsg("车间id为空");

        List<Equipment> equipmentList = equipmentService.getEquipBySubFactoryId(subFactoryId);

        return JSONResult.ok(equipmentList);
    }
    /**
     * 根据设备主键查询设备
     * @param id
     * @return
     */
    @ApiImplicitParam(name = "id", value = "设备id", required = true,
            dataType = "string", paramType = "query")
    @ApiOperation(value = "查询设备", notes = "查询设备")
    @GetMapping("/getEquipById")
    public JSONResult getEquipById(String id){

        if(StringUtils.isBlank(id))
            return JSONResult.errorMsg("设备id为空");

        Equipment equipmentList = equipmentService.getEquipById(id);

        return JSONResult.ok(equipmentList);
    }
}
